﻿Imports System.Drawing
Imports MordecaiHunter.CommonCodeLibrary.Tools.NDS

Namespace Tools
    Public Class clsNDSFunctions
        Public Shared Function getIcon(ByVal sFullFilePath As String) As Bitmap
            Dim pBinaryReader As New IO.BinaryReader(IO.File.OpenRead(sFullFilePath))
            Dim pStream As IO.Stream = pBinaryReader.BaseStream

            Dim lIconOffset As Long

            Dim pIconOffsetData(3) As Byte
            pStream.Seek(&H68, IO.SeekOrigin.Begin)
            pStream.Read(pIconOffsetData, 0, 4)
            lIconOffset = clsByteReaderFunctions.GetDataPartToInteger(pIconOffsetData, 0, 4)

            Dim pData(&H1000 - 1) As Byte
            pStream.Seek(lIconOffset, IO.SeekOrigin.Begin)
            pStream.Read(pData, 0, &H1000)

            Dim pIcon As Bitmap = Nothing
            pIcon = getIcon(pData)

            Return pIcon
        End Function

        Public Shared Function getIcon(ByVal pHeaderData() As Byte) As Bitmap
            Return _getIcon(clsByteReaderFunctions.GetDataPart(pHeaderData, &H20, &H200), _getPalette(clsByteReaderFunctions.GetDataPart(pHeaderData, &H220, &H20)))
        End Function

        Private Shared Function _getPalette(ByVal data() As Byte) As clsColor()
            Dim palette(15) As clsColor

            Dim currentOffset As Integer
            Dim i As Integer
            Dim currentData(1) As Byte

            currentOffset = 2
            i = 1

            palette(0) = New clsColor()

            Do While currentOffset < data.Count
                currentData = clsByteReaderFunctions.GetDataPart(data, currentOffset, 2)

                palette(i) = New clsColor(currentData)

                currentOffset += 2
                i += 1
            Loop

            Return palette
        End Function

        Private Shared Function _getIcon(ByVal data() As Byte, ByVal pColorPalette() As clsColor) As Bitmap
            Dim pOutIcon As Bitmap

            Dim iCurrentOffset As Integer
            Dim i As Integer
            Dim pCurrentData(31) As Byte

            Dim pTiles(15) As clsTile

            Dim pTileData(63) As Integer
            Dim pCurrentByte As Integer

            iCurrentOffset = 0
            i = 0

            ' Per Tile
            Do While iCurrentOffset < data.Count
                pCurrentData = clsByteReaderFunctions.GetDataPart(data, iCurrentOffset, 32)

                For j = 0 To 31
                    pCurrentByte = clsByteReaderFunctions.GetDataPartToInteger(pCurrentData, j, 1)
                    pTileData(j * 2 + 0) = pCurrentByte Mod 16
                    pTileData(j * 2 + 1) = pCurrentByte \ 16
                Next

                pTiles(i) = New clsTile(pTileData)

                iCurrentOffset += 32
                i += 1
            Loop

            Dim xBig, yBig As Integer
            Dim xSmall, ySmall As Integer
            pOutIcon = New Bitmap(32, 32)

            For i = 0 To 1023
                xBig = ((i \ 8) Mod 4)
                yBig = ((i \ 256) Mod 4)

                xSmall = i Mod 8
                ySmall = (i - ((xBig * 8) + (yBig * 256))) \ 32

                pOutIcon.SetPixel(i Mod 32, i \ 32, pColorPalette(pTiles(xBig + yBig * 4).Pixel(xSmall, ySmall)).Color)
            Next

            Return pOutIcon
        End Function
    End Class
End Namespace

